const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 2, //餐桌号
    people: '', //人数
    id: '', //餐厅id
    table: -1, //餐桌下标
    tableNum: [], //餐桌信息
    seats: '0', //选择的就餐桌人数
    select_tables: '', //选择的餐桌
    sid: '', //餐桌id
    types: '', //就餐方式
    hidden: true,
    duration: 2000,
    hidden:'false',
    $zanui: {
      toptips: {
        show: false
      }
    },
    num: [{
        people: 1,
      },
      {
        people: 2,
      },
      {
        people: 3,
      },
      {
        people: 4,
      },
      {
        people: 5,
      },
      {
        people: 6,
      },
      {
        people: 7,
      },
      {
        people: 8,
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let id = options.restaurantId
    let types = options.types
    this.setData({
      id: id,
      types: types
    })
    this.get_table_message()
  },
  //选择就餐人数
  select_nums(e) {
    var that = this
    that.setData({
      current: e.currentTarget.dataset.index
    })
    that.get_table_message()
  },
  //查看桌子信息
  get_table_message() {
    let current = this.data.current
    let id = this.data.id
    app.gets(app.url.getTableList + '?restaurant_id=' + id + '&seats=' + current).then(res => {
      this.setData({
        tableNum: res.data.tablelist,
        hidden:true
      })
    })
  },
  //选择桌子

  select_table(e) {
    var that = this
    that.setData({
      table: e.currentTarget.dataset.index,
      select_tables: e.currentTarget.dataset.selecttable,
      seats: e.currentTarget.dataset.seats,
      sid: e.currentTarget.dataset.sid
    })
    // console.log(that.data.select_tables, that.data.seats, that.data.sid)
  },
  //点餐
  ordering() {
    var that = this
    var cantingid = that.data.id
    var sid = that.data.sid
    var userid = app.globalDataO.openid.objectSid
    var types = that.data.types
    var people = that.data.seats
    if (sid == "" || people == "") {
      that._topCue('请选择人数或者桌号')
    } else {
      app.gets(app.url.DCopenTable + '?restaurantId=' + cantingid + '&memberId=' + userid + '&deskId=' + sid + '&type=' + types + '&seats=' + people).then(res => {
        console.log(res)
        if (res.data.message == 1) {
          let deskid = res.data.deskId
          let restaurantId = res.data.restaurantId
          wx.navigateTo({
            url: '../../pages/food/food?deskId=' + deskid + '&restaurantId=' + restaurantId + '&types=' + that.data.types +'&bill_id=0',
          })
        } else {
          that._topCue('系统错误')
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  _topCue: function(text) {
    this.setData({
      content: text,
      $zanui: {
        toptips: {
          show: true
        }
      }
    });
    setTimeout(() => {
      this.setData({
        $zanui: {
          toptips: {
            show: false
          }
        }
      });
    }, this.data.duration);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})